package controller.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import model.domain.Project;

public class ProjectConverter implements Converter  {

    public ProjectConverter(){

    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value)
    {

        if (value == null || (value.trim().length() == 0))
        {
            return value;
        }

        Project project = new Project();

        String[] splitedString = value.split(" - ");
        project.setProjectID(Integer.parseInt(splitedString[0]));
        project.setProjectName(splitedString[1]);

        return project;
    }

    @Override
      public String getAsString(FacesContext context, UIComponent component, Object value)
      {

       Project project = null;

        if (value instanceof Project)
        {
            project = (Project)value;

            StringBuilder projectAsString = new StringBuilder();
            projectAsString.append(project.getProjectID());
            projectAsString.append(" - ");
            projectAsString.append(project.getProjectName());
            String string = projectAsString.toString();
            return string;
        }
        return "";
    }

}
